<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');

$billMst    = array();
$billDetail = array();

if(isset($_POST['submitBtn']))
{
	$partyTinNo  = ($_POST['partyTinNo'] > 0) ? $_POST['partyTinNo'] : 0;
	$billNo      = ($_POST['billNo'] > 0) ? $_POST['billNo'] : 0;
	$amountTotal = ($_POST['amountTotal'] > 0) ? $_POST['amountTotal'] : 0;
	$vat         = ($_POST['vat'] > 0) ? $_POST['vat'] : 0;
	$vatTotal    = ($_POST['vatTotal'] > 0) ? $_POST['vatTotal'] : 0;
	$cst         = ($_POST['cst'] > 0) ? $_POST['cst'] : 0;
	$cstTotal    = ($_POST['cstTotal'] > 0) ? $_POST['cstTotal'] : 0;
	$grossTotal  = ($_POST['grossTotal'] > 0) ? $_POST['grossTotal'] : 0;
	
	if($_POST['billId'] == '')
	{
		$insertBillMst = "INSERT INTO billmstom (partyName, partyAdd, partyTinNo, billNo, billDate, totalAmount, vatInPer, vatTotal, cstInPer, cstTotal,
		                                       grossTotal, amountInWord,invoice, notes)
		                  VALUES ('".addslashes($_POST['partyName'])."','".addslashes($_POST['partyAddress'])."','".$partyTinNo."','".$billNo."',
		                         '".$_POST['billDateYear']."-".$_POST['billDateMonth']."-".$_POST['billDateDay']."',".$amountTotal.",".$vat.",
		                         ".$vatTotal.",".$cst.",".$cstTotal.",".$grossTotal.",'".$_POST['amountInWord']."','".$_POST['invoice']."',
		                         '".$_POST['notes']."')";
	  $insertBillMstRes = mysql_query($insertBillMst);
	  $billMstId = mysql_insert_id();
  }
  else
  {
  	$updateBillMst = "UPDATE billmstom SET partyName = '".addslashes($_POST['partyName'])."', partyAdd = '".addslashes($_POST['partyAddress'])."',
  	                         partyTinNo = '".$partyTinNo."', billNo = '".$billNo."', billDate = '".$_POST['billDateYear']."-".$_POST['billDateMonth']."-".$_POST['billDateDay']."',
  	                         totalAmount = ".$amountTotal.", vatInPer = ".$vat.", vatTotal = ".$vatTotal.", cstInPer = ".$cst.",
  	                         cstTotal = ".$cstTotal.", grossTotal = ".$grossTotal.", amountInWord = '".$_POST['amountInWord']."',
  	                         invoice = '".$_POST['invoice']."',notes = '".$_POST['notes']."'
  	                   WHERE billMstId = ".$_POST['billId'];
  	$updateBillMstRes = mysql_query($updateBillMst);
  	
  	$deleteBillDetail = "DELETE FROM billmstdetailom
                          WHERE billMstId = ".$_POST['billId'];
  	$deleteBillDetailRes = mysql_query($deleteBillDetail);
  	$billMstId = $_POST['billId'];
  }
  
  $loopCount = 0;
  while($loopCount < count($_POST['particulars']))
  {
  	$particulars = $_POST['particulars'][$loopCount];
  	$hnsCode     = ($_POST['hnsCode'][$loopCount] != '') ? $_POST['hnsCode'][$loopCount] : 0;
  	$grossWeight = ($_POST['grossWeight'][$loopCount] != '') ? $_POST['grossWeight'][$loopCount] : 0;
  	$fineWeight  = ($_POST['fineWeight'][$loopCount] != '') ? $_POST['fineWeight'][$loopCount] : 0;
  	$rate        = ($_POST['rate'][$loopCount] != '') ? $_POST['rate'][$loopCount] : 0;
  	$amount      = ($_POST['amount'][$loopCount] != '') ? $_POST['amount'][$loopCount] : 0;
  	
  	if($particulars != '' || $hnsCode != '' || $grossWeight != '' || $fineWeight != '' || $rate != '' || $amount != '')
  	{
	  	$insertBillMstDetail = "INSERT INTO billmstdetailom (billMstId, particular, hnsCode, grossWeight, fineWeight, rate, amount)
	  	                        VALUES (".$billMstId.",'".addslashes($particulars)."',".$hnsCode.",".$grossWeight.",".$fineWeight.",".$rate.",".$amount.")";
	  	$insertBillMstDetailRes = mysql_query($insertBillMstDetail);
  	}
  	$loopCount++;
  }
  header("location:billOriginalOmList.php");
}

if(isset($_GET['billId']))
{
	$selectBillMst = "SELECT billMstId, partyName, partyAdd, partyTinNo, billNo, billDate, totalAmount, vatInPer, vatTotal, cstInPer, cstTotal, grossTotal, 
	                         amountInWord, invoice, notes
	                    FROM billmstom
	                   WHERE billMstId = ".$_GET['billId'];
  $selectBillMstRes = mysql_query($selectBillMst);
  if($billMstRow = mysql_fetch_assoc($selectBillMstRes))
  {
  	$billMst['billMstId']    = $billMstRow['billMstId'];
  	$billMst['partyName']    = $billMstRow['partyName'];
  	$billMst['partyAdd']     = $billMstRow['partyAdd'];
  	$billMst['partyTinNo']   = $billMstRow['partyTinNo'];
  	$billMst['billNo']       = $billMstRow['billNo'];
  	$billMst['billDate']     = $billMstRow['billDate'];
  	$billMst['totalAmount']  = $billMstRow['totalAmount'];
  	$billMst['vatInPer']     = $billMstRow['vatInPer'];
  	$billMst['vatTotal']     = $billMstRow['vatTotal'];
  	$billMst['cstInPer']     = $billMstRow['cstInPer'];
  	$billMst['cstTotal']     = $billMstRow['cstTotal'];
  	$billMst['grossTotal']   = $billMstRow['grossTotal'];
  	$billMst['amountInWord'] = $billMstRow['amountInWord'];
  	$billMst['invoice']      = $billMstRow['invoice'];
  	$billMst['notes']        = $billMstRow['notes'];
  }
  
  $selectBillDetail = "SELECT particular, hnsCode, grossWeight, fineWeight, rate, amount 
                         FROM billmstdetailom
                        WHERE billMstId = ".$_GET['billId'];
  $selectBillDetailRes = mysql_query($selectBillDetail);
  $a = 0;
  while($billDetailRow = mysql_fetch_assoc($selectBillDetailRes))
  {
  	$billDetail[$a]['particular']  = $billDetailRow['particular'];
  	$billDetail[$a]['hnsCode']     = $billDetailRow['hnsCode'];
  	$billDetail[$a]['grossWeight'] = $billDetailRow['grossWeight'];
  	$billDetail[$a]['fineWeight']  = $billDetailRow['fineWeight'];
  	$billDetail[$a]['rate']        = $billDetailRow['rate'];
  	$billDetail[$a]['amount']      = $billDetailRow['amount'];
  	$a++;
  }
}

$smarty = new Smarty;
require("bottom.php");

$smarty->assign("billMst",$billMst);
$smarty->assign("billDetail",$billDetail);
$smarty->display("billOriginalOm.tpl");
?>